/*
 * @Author: CB
 * @Date: 2023-08-01 10:47:42
 * @Description: 安卓广告管理
 */
import { oops } from "db://oops-framework/core/Oops";
import { PluginAds } from "../PluginAds";
import { JAVAFilePathEnum, JAVA_JSMethodEnum, JS_JAVAMethodEnum } from "../define/PluginDefine";


export class PluginAdsAndroid extends PluginAds {
    /** 显示banner广告 */
    showBannerAD() {
        this.callStaticMethod(JAVAFilePathEnum.AdsHelper, JS_JAVAMethodEnum.ShowBannerAd, '()V');
    }

    /** 隐藏banner广告 */
    hideBannerAD() {
        this.callStaticMethod(JAVAFilePathEnum.AdsHelper, JS_JAVAMethodEnum.hideBannerAd, '()V');
    }

    /** 显示激励视频广告 */
    showRewardAD(finishCallback?: (success: boolean) => void, errorCallback?: (error: string) => void) {
        oops.message.once(JAVA_JSMethodEnum.RewardVideoCallback, (eventData: any) => {
            oops.log.trace(`激励视频广告播放结果 ${eventData}`);
            try {
                const dataObj = JSON.parse(eventData);
                if (dataObj) {
                    if (dataObj.status === 0) {
                        oops.log.trace(`激励视频成功 ${eventData}`);
                        finishCallback && finishCallback(true);
                    } else if (dataObj.status === 1) {
                        oops.log.trace(`激励视频失败 ${eventData}`);
                        finishCallback && finishCallback(false);
                    } else {
                        errorCallback && errorCallback('未知错误');
                    }
                } else {
                    errorCallback && errorCallback('数据格式错误');
                }
            } catch (error) {
                errorCallback && errorCallback('数据格式错误');
            }
        }, this);
        this.callStaticMethod(JAVAFilePathEnum.AdsHelper, JS_JAVAMethodEnum.showRewardAd, '()V');
    }

    /** 显示插屏广告 */
    showInterstitialAd(callback?: () => void) {
        oops.message.once(JAVA_JSMethodEnum.InterstitialAdCallJS, (eventData: any) => {
            oops.log.trace(`插屏广告播放结果 ${eventData}`,);
            callback && callback();
        }, this);
        this.callStaticMethod(JAVAFilePathEnum.AdsHelper, JS_JAVAMethodEnum.showInterstitialAd, '()V');
    }

}